#include "game.h"
#include "display.c"
#include "btns.c"
#include "uart.c"
#include "timer.c"
#include "game.c"

// 定时器0中断服务函数，用于处理方块下落
void timer0_ISR() interrupt 1
{
    static unsigned int count = 0;
    count++;
    if (count >= game_setting.speed) // 根据游戏速度控制下落频率
    {
        count = 0;
        game_zone_remove_shape();
        if (game_zone_check_shape(game_shape.x, game_shape.y + 1, game_shape.shape))
        {
            game_shape.y++;
        }
        else
        {
            // 方块落地，生成新方块
            game_zone_add_shape();
            game_zone_new_shape();
            if (!game_zone_check_shape(game_shape.x, game_shape.y, game_shape.shape))
            {
                // 游戏结束
                display_game_over();
                while (1);
            }
        }
        game_zone_add_shape();
        display_refresh_game_zone();
    }
}

void main()
{
    // 初始化游戏区域
    game_zone_data_init();
    // 初始化显示
    display_init();
    // 显示游戏开始界面
    display_game_start();

    // 等待开始按键按下
    unsigned char btn_code;
    while (1)
    {
        btn_code = btn_scan();
        if (btn_code == BTN_CODE_START)
        {
            break;
        }
    }

    // 显示速度选择界面
    display_speed_selection();
    while (1)
    {
        btn_code = btn_scan();
        if (btn_code == BTN_CODE_LEFT)
        {
            if (game_setting.speed > 1)
            {
                game_setting.speed--;
                display_speed_in_selection();
            }
        }
        else if (btn_code == BTN_CODE_RIGHT)
        {
            if (game_setting.speed < 10)
            {
                game_setting.speed++;
                display_speed_in_selection();
            }
        }
        else if (btn_code == BTN_CODE_START)
        {
            break;
        }
    }

    // 初始化游戏页面
    display_init_game_page();
    // 生成新方块
    game_zone_new_shape();
    game_zone_add_shape();
    display_refresh_game_zone();

    // 初始化定时器0
    TMOD |= 0x01; // 定时器0工作在模式1
    TH0 = (65536 - 1000) / 256; // 定时1ms
    TL0 = (65536 - 1000) % 256;
    ET0 = 1; // 使能定时器0中断
    EA = 1;  // 使能全局中断
    TR0 = 1; // 启动定时器0

    // 主循环，处理按键事件
    while (1)
    {
        btn_code = btn_scan();
        if (btn_code == BTN_CODE_PAUSE)
        {
            if (TR0)
            {
                TR0 = 0; // 暂停定时器
                display_pause();
            }
            else
            {
                TR0 = 1; // 恢复定时器
                display_pause_hide();
            }
        }
        else if (btn_code == BTN_CODE_UP)
        {
            // 方块旋转
            game_zone_remove_shape();
            unsigned int new_shape = TETRIS_SHAPES[game_shape.type][(game_shape.pos + 1) % 4];
            if (game_zone_check_shape(game_shape.x, game_shape.y, new_shape))
            {
                game_shape.pos = (game_shape.pos + 1) % 4;
                game_shape.shape = new_shape;
            }
            game_zone_add_shape();
            display_refresh_game_zone();
        }
        else if (btn_code == BTN_CODE_DOWN)
        {
            // 方块加速下落
            game_zone_remove_shape();
            if (game_zone_check_shape(game_shape.x, game_shape.y + 1, game_shape.shape))
            {
                game_shape.y++;
            }
            else
            {
                game_zone_add_shape();
                game_zone_new_shape();
                if (!game_zone_check_shape(game_shape.x, game_shape.y, game_shape.shape))
                {
                    // 游戏结束
                    display_game_over();
                    while (1);
                }
            }
            game_zone_add_shape();
            display_refresh_game_zone();
        }
        else if (btn_code == BTN_CODE_LEFT)
        {
            // 方块左移
            game_zone_remove_shape();
            if (game_zone_check_shape(game_shape.x - 1, game_shape.y, game_shape.shape))
            {
                game_shape.x--;
            }
            game_zone_add_shape();
            display_refresh_game_zone();
        }
        else if (btn_code == BTN_CODE_RIGHT)
        {
            // 方块右移
            game_zone_remove_shape();
            if (game_zone_check_shape(game_shape.x + 1, game_shape.y, game_shape.shape))
            {
                game_shape.x++;
            }
            game_zone_add_shape();
            display_refresh_game_zone();
        }
    }
}